<?php

namespace Cck\ArticlesSystem\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class RoleResource extends JsonResource
{
    public function toArray($request)
    {
        if (is_null($this->resource)) {
            return [];
        }

        $result = $this->resource->toArray();

        $result['permissions'] = $this->whenLoaded('permissions', function () {
            return PermissionResource::collection($this->resource->permissions);
        });

        return $result;
    }
}
